package com.fanshuai.springcloud.config;

import com.fanshuai.springcloud.handler.HelloHandler;
import com.fanshuai.springcloud.service.BookService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

import javax.annotation.Resource;

@Configuration
public class MainConfig {
    @Resource
    private HelloHandler helloHandler;

    @Bean(name = "baseBookService")
    @ConditionalOnMissingBean(BookService.class)
    public BookService bookService() {
        return new BookService();
    }

    @Bean
    public RouterFunction<ServerResponse> helloRouter() {
        return RouterFunctions.route(RequestPredicates.GET("/hello"), helloHandler::hello)
                .andRoute(RequestPredicates.GET("/hello2"), serverRequest -> helloHandler.hello2(serverRequest));
    }
}
